Skip to content

fix: guard LLM response access before dereferencing choices/content#92

Open
qizwiz wants to merge 1 commit into
HKUDS:mainfrom
qizwiz:fix/llm-response-unguarded
Open

fix: guard LLM response access before dereferencing choices/content#92
qizwiz wants to merge 1 commit into
HKUDS:mainfrom
qizwiz:fix/llm-response-unguarded

Conversation

@qizwiz

@qizwiz qizwiz commented May 18, 2026

Copy link
Copy Markdown

What

Guard LLM response access in three files before dereferencing .choices[0] or .content[0].

Why

response.choices[0].message.content is typed Optional[str] in the OpenAI SDK — it returns None (not raises) when finish_reason is "tool_calls" or the provider applies content filtering. Similarly, the Anthropic SDK can return an empty content list on filtered responses, making message.content[0].text unsafe without a length check.

autoagent/environment/mdconvert.py and autoagent/environment/markdown_browser/mdconvert.py (OpenAI):

# before
return response.choices[0].message.content

# after
if not response.choices or response.choices[0].message is None or response.choices[0].message.content is None:
    raise ValueError("LLM returned empty or filtered response")
return response.choices[0].message.content

docs/translation_updater.py (Anthropic):

# before
return message.content[0].text

# after
if not message.content or message.content[0].text is None:
    raise ValueError("LLM returned empty or filtered response")
return message.content[0].text

Test plan

  • Verify translate_content() raises ValueError when given a mock message.content = []
  • Verify the mdconvert LLM path raises ValueError when given a mock response.choices = []

🤖 Generated with Claude Code

response.choices[0].message.content is Optional[str] in the OpenAI SDK —
returns None (not raises) when finish_reason is tool_calls or the provider
filters output. message.content[0].text is similarly unsafe on the
Anthropic side when content is empty or filtered.

Adds guards in three files:
- autoagent/environment/mdconvert.py (OpenAI)
- autoagent/environment/markdown_browser/mdconvert.py (OpenAI)
- docs/translation_updater.py (Anthropic)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant